package com.hxkcloud.nurse.fragments;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.DataSetObserver;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.YjhApplication;
import com.hxkcloud.nurse.activitys.ArticleDetailActivity;
import com.hxkcloud.nurse.activitys.NewDolistActivity;
import com.hxkcloud.nurse.adapter.NoticeDoAdapter;
import com.hxkcloud.nurse.beans.DolistBean;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.nurse.utils.BrightnessUtils;
import com.hxkcloud.nurse.views.pulltorefreshlayout.PullToRefreshLayout;
import com.hxkcloud.nurse.views.pulltorefreshlayout.PullableExpandableListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by william_sun on 2016-07-09.
 * Description:提醒页下的用户执行单
 */
@SuppressLint("ValidFragment")
public class NoticeDoFragment extends BaseFragment implements PullToRefreshLayout.OnRefreshListener, NoticeDoAdapter.onDolistFunctionListener, View.OnClickListener {
    private View view;
    private PullToRefreshLayout refreshLayout;
    private PullableExpandableListView eListView;

    private List<DolistBean> dolists;
    private int listType = 0;//0执行单 1提醒单
    private String patientId;
    private NoticeDoAdapter adapter;

    //两个按钮
    private PopupWindow popup_easynotice;
    private TextView tv_easynotice_title;
    private TextView tv_easynotice_cancel;
    private TextView tv_easynotice_func;
    private String orderId;//删除、修改

    private DataSetObserver observer = new DataSetObserver() {
        @Override
        public void onChanged() {
            super.onChanged();
        }

        @Override
        public void onInvalidated() {
            super.onInvalidated();
        }
    };

    public NoticeDoFragment(int type, String pId) {
        this.listType = type;
        this.patientId = pId;

        dolists = new ArrayList<>();
//        for (int i = 0; i < 5; i++) {
//            DolistBean bean = new DolistBean();
//            bean.setName("NAME" + i);
//            List<DolistItemBean> items = new ArrayList<>();
//            for (int j = 0; j < 5; j++) {
//                DolistItemBean item = new DolistItemBean();
//                item.setDate("2016.6.9");
//                item.setDayIndex("第二天" + j);
//                List<NoticeFunctionBean> funcs = new ArrayList<>();
//                for (int m = 0; m < 3; m++) {
//                    NoticeFunctionBean func = new NoticeFunctionBean();
//                    func.setName("血压" + m);
//                    func.setType(1);
//                    funcs.add(func);
//                }
//                item.setFunctions(funcs);
//                List<String> times = new ArrayList<>();
//                for (int m = 0; m < 3; m++) {
//                    String time = "09:0" + m;
//                    times.add(time);
//                }
//                item.setTimes(times);
//                items.add(item);
//            }
//            bean.setItems(items);
//            dolists.add(bean);
//        }

    }

    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_DOORDERUSERLIST_SUCCESS:
                dolists = (List<DolistBean>) msg.obj;
                adapter.setDatas(dolists);
                refreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                break;
            case MessageCode.NETWORKCODE_DOORDERUSERLIST_FAILURE:
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                } else {
                    toastMsg(R.string.datagetfail);
                }
                refreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                break;
            case MessageCode.NETWORKCODE_DOORDERDELETE_SUCCESS:
                loading();
                YiJiaHuNetWorkUtils.doOrderUserList(listType, patientId, handler);
                break;
            case MessageCode.NETWORKCODE_DOORDERDELETE_FAILURE:
                if (msg.obj != null) {
                    toastMsg(msg.obj.toString());
                } else {
                    toastMsg(R.string.datadeletefail);
                }
                break;
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_noticedo, null);
        initView();
        initPopup();

        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    @Override
    protected void initView() {
        refreshLayout = (PullToRefreshLayout) view.findViewById(R.id.refreshlayout_noticedo);
        eListView = (PullableExpandableListView) view.findViewById(R.id.recyclerview_noticeremind);

//        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext);
//        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//        eListView.setLayoutManager(mLayoutManager);
        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setCanPull(true, false);

        adapter = new NoticeDoAdapter(mContext, eListView, listType, dolists, this);
        adapter.registerDataSetObserver(observer);
        eListView.setAdapter(adapter);

        eListView.setGroupIndicator(null);
//        eListView.setChildDivider(ContextCompat.getDrawable(mContext, R.drawable.shape_null));
    }

    private void initPopup() {
        popup_easynotice = new PopupWindow(mContext);
        View viewNotice = LayoutInflater.from(mContext).inflate(R.layout.popup_easynotice, null);
        popup_easynotice.setContentView(viewNotice);

        popup_easynotice.setWidth((int) (screenWidth * 0.7));
        popup_easynotice.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popup_easynotice.setFocusable(true);
        popup_easynotice.setTouchable(true);
        popup_easynotice.setOutsideTouchable(true);
        popup_easynotice.setBackgroundDrawable(new ColorDrawable(0x00000000));
        popup_easynotice.setAnimationStyle(R.style.popupwindow_anim_alpha);

        tv_easynotice_title = (TextView) viewNotice.findViewById(R.id.tv_easynotice_title);
        tv_easynotice_cancel = (TextView) viewNotice.findViewById(R.id.tv_easynotice_cancel);
        tv_easynotice_func = (TextView) viewNotice.findViewById(R.id.tv_easynotice_func);

        tv_easynotice_title.setText(R.string.suredeletethisitem);
        tv_easynotice_cancel.setOnClickListener(this);
        tv_easynotice_func.setOnClickListener(this);

        popup_easynotice.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                BrightnessUtils.lightOn(activity);
            }
        });
    }

    public void expandFirstGroup() {
        if (dolists.size() > 0) {
            eListView.expandGroup(0);
        }
    }

    @Override
    protected void initListener() {
        //展开
        eListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupPosition) {

            }
        });
        //折叠
        eListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
            @Override
            public void onGroupCollapse(int groupPosition) {

            }
        });
    }

    @Override
    protected void initData() {
        loading();
        YiJiaHuNetWorkUtils.doOrderUserList(listType, patientId, handler);
    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        loading();
        YiJiaHuNetWorkUtils.doOrderUserList(listType, patientId, handler);

    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
    }

    //查看列表文章
    @Override
    public void article(String id) {
        Intent iArticle = new Intent(mContext, ArticleDetailActivity.class);
        iArticle.putExtra("articleId", id);
        iArticle.putExtra("acctType", YjhApplication.articlekind_course);
        startActivity(iArticle);

    }

    @Override
    public void delete(int pIndex) {
        orderId = dolists.get(pIndex).getOrderId();
        popup_easynotice.showAtLocation(view, Gravity.CENTER, 0, 0);
        BrightnessUtils.lightOff(activity);
    }

    @Override
    public void modify(int pIndex) {
        DolistBean bean = dolists.get(pIndex);
        Intent iModify = new Intent(mContext, NewDolistActivity.class);
        iModify.putExtra("type", listType);
        iModify.putExtra("item", bean);
        iModify.putExtra("patientId", patientId);
        iModify.putExtra("user", true);
        startActivity(iModify);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_easynotice_cancel:
                popup_easynotice.dismiss();
                break;
            case R.id.tv_easynotice_func:
                popup_easynotice.dismiss();
                loading();
                YiJiaHuNetWorkUtils.doOrderDelete(orderId, handler);
//                dolists.remove(0);
//                adapter.setDatas(dolists);
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        adapter.unregisterDataSetObserver(observer);
    }
}
